#include "../../common/mysql_relation.hpp"
#include "../../common/mysql_friend_apply.hpp"
#include "../../common/mysql_chat_session.hpp"
#include "../../common/utils.hpp"

std::shared_ptr<odb::core::database> db;
RelationTable::ptr relation_table;
FriendApplyTable::ptr friend_apply_table;
ChatSessionTable::ptr chat_session_table;
void TestRelationInsert() {
    relation_table->insert("用户ID1", "用户ID2");
    relation_table->insert("用户ID1", "用户ID3");
    relation_table->insert("用户ID1", "用户ID4");
    relation_table->insert("用户ID2", "用户ID3");
    relation_table->insert("用户ID2", "用户ID4");
    relation_table->insert("用户ID3", "用户ID4");
}

void TestRelationRemove() {
    relation_table->remove("用户ID1", "用户ID2");
}
void TestRelation() {
    // relation_table->insert("用户ID1", "用户ID2");
    // std::cout << relation_table->exists("用户ID1", "用户ID2");
    // relation_table->remove("用户ID1", "用户ID2");
    auto ids = relation_table->friends("用户ID3");
    for(auto &id : ids) {
        std::cout << id << std::endl;
    }
}

void TestFriendApply() {
    FriendApply apply1("event_id1", "用户ID1", "用户ID2");
    // friend_apply_table->insert(apply1);
    // std::cout << friend_apply_table->exists("用户ID1", "用户ID2") << std::endl;
    // friend_apply_table->remove("用户ID1", "用户ID2");
    // std::cout << friend_apply_table->exists("用户ID1", "用户ID2") << std::endl;

    FriendApply apply2("event_id2", "用户ID1", "用户ID3");
    friend_apply_table->insert(apply2);
    FriendApply apply3("event_id3", "用户ID1", "用户ID4");
    friend_apply_table->insert(apply3);
    FriendApply apply4("event_id4", "用户ID2", "用户ID3");  
    friend_apply_table->insert(apply4);
    FriendApply apply5("event_id5", "用户ID2", "用户ID4");
    friend_apply_table->insert(apply5);
    FriendApply apply6("event_id6", "用户ID3", "用户ID4");
    friend_apply_table->insert(apply6);
    auto applies = friend_apply_table->applyUsers("用户ID3");
    for(auto &apply_id : applies) {
        std::cout << apply_id << std::endl;
    }
}


void TestChatSession() {
    // ChatSession cs1("sessino1", "群聊会话1", ChatSessionType::GROUP);
    // chat_session_table->insert(cs1);
    // ChatSession cs2("sessino2", "群聊会话2", ChatSessionType::GROUP);
    // chat_session_table->insert(cs2);
    // auto cs = chat_session_table->select("cs_id1");
    // std::cout << cs->chat_session_id() << " " << cs->chat_session_name() << " " << (int)cs->chat_session_type() << std::endl;
    // chat_session_table->remove("cs_id1");
    // ChatSession cs3("session_roboko", "roboko单聊会话", ChatSessionType::SINGLE);
    // chat_session_table->insert(cs3);
    // ChatSession cs4("7651-7470d73d-0000", "单聊会话2", ChatSessionType::SINGLE);
    // chat_session_table->insert(cs4);
    // auto ret = chat_session_table->singleChatSession("6919-9f03ea75-0000");
    // for(auto &s : ret) {
    //     std::cout << s._chat_session_id << " " << s._friend_id << std::endl;
    // }
    auto res = chat_session_table->groupChatSession("uid4");
    for(auto &s : res) {
        std::cout << "-----------\n";
        std::cout << s._chat_session_id << " " << s._chat_session_name << std::endl;
    }
    chat_session_table->remove("uid5", "uid4");
}
int main() {
    init_logger();
    db = ODBFactory::create(
        "root",
        "&a==nullPTR1",
        "chat_system"
    );
    relation_table = std::make_shared<RelationTable>(db);
    friend_apply_table = std::make_shared<FriendApplyTable>(db);
    chat_session_table = std::make_shared<ChatSessionTable>(db);
    // TestRelation();
    // TestFriendApply();
    TestChatSession();
    return 0;
}
